package deconstruction.common;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.ShapedRecipes;
import net.minecraft.item.crafting.ShapelessRecipes;
import net.minecraftforge.classloading.FMLForgePlugin;
import net.minecraftforge.common.Configuration;
import net.minecraftforge.common.Property;
import cpw.mods.fml.client.FMLClientHandler;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.FMLDummyContainer;
import cpw.mods.fml.common.FMLModContainer;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.discovery.ModDiscoverer;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;
import cpw.mods.fml.common.registry.TickRegistry;
import cpw.mods.fml.relauncher.Side;
import deconstruction.additionalModSupport.ic2;
import deconstruction.deconTable.DeconstructionManager;
import deconstruction.deconTable.PacketHandler;
import deconstruction.localization.LocalizationHandler;



@Mod(modid=Info.MOD_ID, name=Info.NAME, version=Info.VERSION)
@NetworkMod(clientSideRequired=true, serverSideRequired=false, channels={"DeconChannel"}, packetHandler = PacketHandler.class)
public class mod_Deconstruction {
	public static int[] DisabledIDS = new int[1];
	public static String[] DisabledRecipes;

	public static Block deconstructionTable;
	public static int TableID = 600;

	private GuiHandler guiHandler = new GuiHandler();

	public static Logger log;

	public static boolean isIDBlacklisted(int id){
		ArrayList list = new ArrayList();
		for(int i:DisabledIDS)
			list.add(i);
		return list.contains(id);
	}

	@Instance(Info.MOD_ID)
	public static mod_Deconstruction instance;

	@SidedProxy(clientSide="deconstruction.client.ClientProxy", serverSide="deconstruction.common.CommonProxy")
	public static CommonProxy proxy;

	@EventHandler
	public void preInit(FMLPreInitializationEvent event) {
		log = event.getModLog();

		LocalizationHandler.loadLanguages();

		//Config
		Info.CONFIG_FILE = event.getSuggestedConfigurationFile();

		Configuration config;
		try{
			config = new Configuration(Info.CONFIG_FILE);
			config.load();
			log.info(new StringBuilder().append("Config loaded from ").append(Info.CONFIG_FILE.getAbsolutePath()).toString());
		} catch (Exception e) {
			log.warning(new StringBuilder().append("Error while trying to access configuration! ").append(e).toString());
			config = null;
		}



		if(config != null){
			Property prop = config.get("block", "Deconstruction Table ID", TableID);
			TableID = prop.getInt(600);

			prop = config.get("Blacklists", "Disabled Ids", DisabledIDS);
			prop.comment = "Not yet implemented";
			DisabledIDS = prop.getIntList();
			prop = config.get("Update Checker","Enable Update Checks?", true);
			prop.comment = "Will Deconstruction Table look for updates when it launches?";
			Info.CHECK_FOR_UPDATES = prop.getBoolean(true);

			prop = config.get("Update Checker", "Last Remote Version", Info.LAST_DISCOVERED_VERSION);
			prop.comment = "The last version the update checker has found from the remote source";
			Info.LAST_DISCOVERED_VERSION = prop.getString();

			prop = config.get("Update Checker", "Attempts", Info.VERSION_CHECK_ATTEMPTS);
			prop.comment = "The ammount of attempts the update checker makes to find an available update before it gives up.";
			Info.VERSION_CHECK_ATTEMPTS = prop.getInt();
			if(config.hasChanged()) config.save();
		}



		// Conduct the version check and log the result
		UpdateHandler.execute();

		// Initialize the Version Check Tick Handler (Client only)
		TickRegistry.registerTickHandler(new VersionCheckTickHandler(), Side.CLIENT);

	}

	@EventHandler
	public void load(FMLInitializationEvent event) {
		

		deconstructionTable = new BlockDeconstructionTable(this.TableID).setHardness(2.5F).setStepSound(Block.soundWoodFootstep);
		GameRegistry.registerBlock(deconstructionTable, "deconstructionTable");

		LanguageRegistry.addName(deconstructionTable, LanguageRegistry.instance().getStringLocalization("block.deconstructionTable.name"));

		GameRegistry.addRecipe(new ItemStack(deconstructionTable, 1), "BDB", "ACA", "AAA", 'A', Block.planks, 'B', Item.ingotIron, 'C', Block.workbench, 'D', Item.diamond);

		NetworkRegistry.instance().registerGuiHandler(this, guiHandler);

	}


	@EventHandler
	public void postInit(FMLPostInitializationEvent event) {
		if(Loader.isModLoaded("IC2")){
			log.info("IC2 is installed; adding additional recipes...");
			ic2.LoadRecipes(DeconstructionManager.getInstance());
		}
		//log.info(DeconstructionManager.getInstance().checkList() +" additional recipes added to decon list.");
		log.info((DeconstructionManager.getInstance().getCompleteRecipeList().size() - 263) +" Other recipes are supported.");
	}

	public void setLastRemoteVers(String remoteVersion) {
		Info.LAST_DISCOVERED_VERSION = remoteVersion;
	}
}